from urllib import parse, request
import json
# import pymongo
from pymongo import MongoClient

# url = "https://api.steampowered.com/IDOTA2Match_570/GetMatchDetails/V001"
# get_param = {'key': 'EFA1E81676FCC47157EA871A67741EF5', 'match_id': '3585112975'}
# get_param = parse.urlencode(get_param)
# url = url + '?' + get_param
# res_data = request.urlopen(url)
# res = res_data.read()
# res_string = str(res, encoding="utf-8")
# res_json = json.loads(res_string)
# print(type(res_json))

# url = 'http://api.steampowered.com/IDOTA2Match_570/GetMatchHistory/v1'
# param = {'game_mode': 3, 'skill': 2, 'start_at_match_id': '3587555089', 'key': 'EFA1E81676FCC47157EA871A67741EF5'}
# param = parse.urlencode(param)
# url = url + '?' + param
# print(url)
# res_data = request.urlopen(url)
# res = res_data.read()
# res_string = str(res, encoding="utf-8")
# res_json = json.loads(res_string)
# print(len(res_json))


url = 'http://api.steampowered.com/IDOTA2Match_570/GetMatchDetails/v1'
# param = {'game_mode': 3, 'skill': 2, 'start_at_match_id': '3587555089', 'key': 'EFA1E81676FCC47157EA871A67741EF5'}
param = {'match_id': '3567996027', 'key': 'EFA1E81676FCC47157EA871A67741EF5'}
param = parse.urlencode(param)
url1 = url + '?' + param
print(url1)
res_data = request.urlopen(url1)
res = res_data.read()
res_string = str(res, encoding="utf-8")
res_json = json.loads(res_string)
print(type(res_json))
print('*****************************')
print(res_json['result'])
print('error' in res_json['result'])

param = {'match_id': '3567996033', 'key': 'EFA1E81676FCC47157EA871A67741EF5'}
param = parse.urlencode(param)
url2 = url + '?' + param
print(url2)
res_data = request.urlopen(url2)
res = res_data.read()
res_string = str(res, encoding="utf-8")
res_json = json.loads(res_string)
print(type(res_json))
print('*****************************')
print(res_json['result'])
print('error' in res_json['result'])

# print(len(res_json['result']))
# 3567996033
# #try mongodb
# client = MongoClient('mongodb://root:19930309@dds-uf6773d77c87f4341.mongodb.rds.aliyuncs.com:3717,dds-uf6773d77c87f4342.mongodb.rds.aliyuncs.com:3717/admin?replicaSet=mgset-4868049')
# # client = MongoClient('mongodb://root:930309!jyd@dds-uf6773d77c87f4341.mongodb.rds.aliyuncs.com:3717')
# db = client.test_database
# collection = db.test_collection
# testdata = res_json['result']
# testdata_id = collection.insert_one(testdata).inserted_id
# print(testdata_id)